/*
 * File ReceivedMsgModel.java 
 */
package cs.model;

import java.util.ArrayList;
import java.util.Observable;

import cs.entity.UserTextMessage;

/**
 *This class represents a received message queue by the network.<p>
 * Package  cs.model
 * Project ChatSystemG1 - INSA 4INFOA1 - 2013/2014
 * @author Julie DUMONS & Juste OUEDRAOGO
 */
public class ReceivedMsgModel extends Observable {
	/**
	 * received text messages queue
	 */
	private ArrayList<UserTextMessage>receivedMsg;
	
	/**
	 * constructor
	 */
	public ReceivedMsgModel(){
		receivedMsg = new ArrayList<UserTextMessage>();
	}
	
	/**
	 * add message to the queue
	 * @param msg New message received with text and user information
	 */
	public void addReceivedMsg(UserTextMessage msg){	
		System.out.println("New received message added to model"); 
		receivedMsg.add(msg);
		setChanged();
		notifyObservers();
	}
	
	/**
	 * return and Remove the first message received
	 * @return first received task or null if the queue is empty
	 */
	public UserTextMessage getRemoveReceivedMsg() {
		
		//check if the queue is empty
		if (!(receivedMsg.isEmpty())) {
			UserTextMessage msgReceived = receivedMsg.get(0); 
			receivedMsg.remove(0);
			return msgReceived ; 
		} else {
			return null ; 
		}
		
	}
	
	/**
	 * Remove all received messages
	 * used when the local user are disconnecting
	 */
	public void removeAllReceivedMsg() {
		receivedMsg.clear(); 
	}

}
